package ch.sv7.tool.dbmanager.util;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import ch.sv7.tool.xml.XMLDocumentWriter;

/**
 * TODO: Document me!
 *
 * @author svt
 *
 */
public class ScenarioReader {

	private URL scenarioURL;
	private InputStream beforeDatabaseStream;
	private InputStream afterDatabaseStream;
	private String sql;
	private boolean reversible; 

	/**
	 * @param scenarioURL
	 */
	public ScenarioReader(URL scenarioURL) throws Exception {
		super();
		this.scenarioURL = scenarioURL;
		read();
	}
	
	public boolean isReversible() {
		return reversible;
	}
	
	private InputStream buildStreamFromNode(Node node) {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		try {
			builder = factory.newDocumentBuilder();
		} catch (ParserConfigurationException pce) {
			System.out.println(pce.getMessage());
			return null;
		}		
		
		Document document = builder.newDocument();
		Node newNode = document.importNode(node, true);
		document.appendChild(newNode);
		
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		PrintWriter printWriter = new PrintWriter(baos);
		XMLDocumentWriter writer = new XMLDocumentWriter(printWriter);
		writer.write(document);
		writer.close();	
		//System.out.println(new String(baos.toByteArray()));
		return new ByteArrayInputStream(baos.toByteArray());
		
	}
	
	private void read() throws Exception {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = null;
		try {
			builder = factory.newDocumentBuilder();
		} catch (ParserConfigurationException pce) {
			System.out.println(pce.getMessage());
			return;
		}
		InputStream stream = null;
		try {
			stream = scenarioURL.openStream();
			Document document = builder.parse(stream);
			Element rootElement = document.getDocumentElement();
			
			reversible = "true".equalsIgnoreCase(rootElement.getAttribute("reversible"));
			
			NodeList nodes = rootElement.getChildNodes();
			for (int i=0; i<nodes.getLength(); i++) {
				Node node = nodes.item(i);
				
				if ("database".equals(node.getNodeName())){
					String type = ((Element)node).getAttribute("type");
					if ("before".equalsIgnoreCase(type)){
						beforeDatabaseStream = buildStreamFromNode(node);
					} else if ("after".equalsIgnoreCase(type)){
						afterDatabaseStream = buildStreamFromNode(node);
					}
				} else if ("data".equals(node.getNodeName())){
					Node textNode = node.getFirstChild();
					if (textNode != null) {
						sql = textNode.getNodeValue();
						//System.out.println(sql);
					}
						
				}
			}
			
		} finally {
			if (stream != null) {
				stream.close();
			}
		}

	}

	public InputStream getBeforeDatabaseStream() {
		return beforeDatabaseStream;
	}

	public InputStream getAfterDatabaseStream() {
		return afterDatabaseStream;
	}

	public String getSql() {
		return sql;
	}
	
	
	
}
